﻿using Microsoft.LightSwitch;
using System.Text;
using System.Linq;
using System.Collections.Generic;
using System;

namespace LightSwitchApplication
{
    public partial class Category
    {
        partial void BooksPerCategory_Compute(ref int result)
        {
            if (this.BookCategories.Any())
            {
                result += this.BookCategories.Count();
            }
        }

        partial void PagesPerCategory_Compute(ref int result)
        {
            if (this.BookCategories.Any())
            {
                List<Book> booksInCategory = (from BookCategory row in this.BookCategories select row.Book).ToList<Book>();

                foreach (Book b in booksInCategory)
                {
                    result += b.NumberOfPages;
                }
            }
        }

        partial void ReadPagesPerCategory_Compute(ref int result)
        {
            if (this.BookCategories.Any())
            {
                List<Book> booksInCategory = (from BookCategory row in this.BookCategories select row.Book).ToList<Book>();

                foreach (Book b in booksInCategory)
                {
                    result += b.ReadTillPageNumber;
                }
            }

        }

        partial void PercentageOfCompletion_Compute(ref decimal result)
        {
            // Set result to the desired field value
            if (this.ReadPagesPerCategory > 0 && this.PagesPerCategory > 0)
            {
                decimal rp = this.ReadPagesPerCategory;
                decimal pc = this.PagesPerCategory;
                result = rp / pc;
            }
        }
    }
}